package com.bao1;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class myaspect {
    @Before(value = "mypt()")
    public void before(){
        System.out.println("小帅啦欢迎您,时间："+new SimpleDateFormat(
                "yyyy" +
                "年MM" +
                "月dd日").format(new Date()));
    }
    @AfterReturning(value = "execution(public void doother(student)" +
            ")")
    public void beforeother(JoinPoint jp){
        student a= (student) jp.getArgs()[0];
        a.age=24;
        System.out.println("已经执行other代理");
    }
    @Around(value = "execution(public String dofrist(int, String))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object o=null;
        System.out.println("环绕通知");
        if ((int)pjp.getArgs()[0]!=22) {
            System.out.println("年龄可能输入有误");
            o = "error";
        }
        else
            o=pjp.proceed();
        System.out.println("环绕通知");
        return o;
    }
    @AfterThrowing(value = "execution(public void dosecond())",throwing = "e")
    public void afterthrowing(Exception e){
        System.out.println("方法发生异常时，执行："+e.getMessage());
    }
    @After(value = "mypt()")
    public void myafter(){
        System.out.println("@After");
    }
    @Pointcut(value = "execution(public void dothree())")
    public void mypt(){

    }
}
